package bean;

public class Checkerboard {
	private int size = 19;
	private int[][] chess;
	int posX;
	int posY;
	int color;

	public int[][] getChess() {
		return chess;
	}

	public void setChess(int[][] chess) {
		this.chess = chess;
	}

	public int getPosX() {
		return posX;
	}

	public void setPosX(int posX) {
		this.posX = posX;
	}

	public int getPosY() {
		return posY;
	}

	public void setPosY(int posY) {
		this.posY = posY;
	}

	public int getColor() {
		return color;
	}

	public void setColor(int color) {
		this.color = color;
	}

	public Checkerboard() {
		chess = new int[19][19];
		for (int i = 0; i < 19; i++) {
			for (int j = 0; j < 19; j++) {
				chess[i][j] = 0;
			}
		}
	}

	public Checkerboard(int[][] chess, int posX, int posY, int color) {
		super();
		this.chess = chess;
		this.posX = posX;
		this.posY = posY;
		this.color = color;
	}

	public void play(int posX, int posY, int color) {
		this.posX = posX;
		this.posY = posY;
		this.color = color;
		chess[posX][posY] = color;
	}

	public boolean fiveJudge() {
		System.out.println("x:"+posX);
		System.out.println("Y:"+posY);
		System.out.println("Color:"+color);		
		return fiveJudge(this.posX, this.posY, this.chess);

	}

	// 判断是否成功
	public boolean fiveJudge(int posX, int posY, int[][] checkerboard) {

		int count = 1;
		// 左上
		for (int i = 0; i < 4; i++) {
			//下一个子的位置
			int x = posX - 1 - i;
			int y = posY + 1 +i;

			// 判断是否出界
			if (x < size && y < size) {
				if (checkerboard[x][y] == checkerboard[posX][posY]) {
					count++;
					if (count > 4) {
						return true;
					}
				} else {
					break;
				}
			}
		}
		// 锟斤拷锟斤拷
		for (int i = 0; i < 4; i++) {
			// 锟斤拷一锟斤拷锟接碉拷位锟斤拷
			int x = posX + 1 +i;
			int y = posY - 1 - i;

			// 锟叫讹拷锟角凤拷锟斤拷锟�
			if (x < size && y < size) {
				if (checkerboard[x][y] == checkerboard[posX][posY]) {
					count++;
					if (count > 4) {
						return true;
					}
				} else {
					break;
				}
			}
		}

		count = 1;
		// 锟斤拷锟斤拷
		for (int i = 0; i < 4; i++) {
			// 锟斤拷一锟斤拷锟接碉拷位锟斤拷
			int x = posX;
			int y = posY + 1 +i;

			// 锟叫讹拷锟角凤拷锟斤拷锟�
			if (x < size && y < size) {
				if (checkerboard[x][y] == checkerboard[posX][posY]) {
					count++;
					if (count > 4) {
						return true;
					}
				} else {
					break;
				}
			}
		}
		// 锟斤拷锟斤拷
		for (int i = 0; i < 4; i++) {
			// 锟斤拷一锟斤拷锟接碉拷位锟斤拷
			int x = posX;
			int y = posY - 1 - i;

			// 锟叫讹拷锟角凤拷锟斤拷锟�
			if (x < size && y < size) {
				if (checkerboard[x][y] == checkerboard[posX][posY]) {
					count++;
					if (count > 4) {
						return true;
					}
				} else {
					break;
				}
			}
		}
		count = 1;
		// 锟斤拷锟斤拷
		for (int i = 0; i < 4; i++) {
			// 锟斤拷一锟斤拷锟接碉拷位锟斤拷
			int x = posX + 1 +i;
			int y = posY + 1 +i;

			// 锟叫讹拷锟角凤拷锟斤拷锟�
			if (x < size && y < size) {
				if (checkerboard[x][y] == checkerboard[posX][posY]) {
					count++;
					if (count > 4) {
						return true;
					}
				} else {
					break;
				}
			}
		}
		// 锟斤拷锟斤拷
		for (int i = 0; i < 4; i++) {
			// 锟斤拷一锟斤拷锟接碉拷位锟斤拷
			int x = posX - 1 - i;
			int y = posY - 1 - i;

			// 锟叫讹拷锟角凤拷锟斤拷锟�
			if (x < size && y < size) {
				if (checkerboard[x][y] == checkerboard[posX][posY]) {
					count++;
					if (count > 4) {
						return true;
					}
				} else {
					break;
				}
			}
		}
		count = 1;
		// 锟斤拷锟斤拷
		for (int i = 0; i < 4; i++) {
			// 锟斤拷一锟斤拷锟接碉拷位锟斤拷
			int x = posX + 1 +i;
			int y = posY;

			// 锟叫讹拷锟角凤拷锟斤拷锟�
			if (x < size && y < size) {
				if (checkerboard[x][y] == checkerboard[posX][posY]) {
					count++;
					if (count > 4) {
						return true;
					}
				} else {
					break;
				}
			}
		}
		// 锟斤拷锟斤拷
		for (int i = 0; i < 4; i++) {
			// 锟斤拷一锟斤拷锟接碉拷位锟斤拷
			int x = posX - 1 - i;
			int y = posY;

			// 锟叫讹拷锟角凤拷锟斤拷锟�
			if (x < size && y < size) {
				if (checkerboard[x][y] == checkerboard[posX][posY]) {
					count++;
					if (count > 4) {
						return true;
					}
				} else {
					break;
				}
			}
		}
		count = 1;
		return false;
	}
}
